package client_logic;
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 



import ocsf.client.*;

import java.io.*;
import java.util.ArrayList;

import client_controllers.AnaliticSystemController;
import client_controllers.CampaignPatternController;
import client_controllers.CampaignReportsController;
import client_controllers.ClassifyProductController;
import client_controllers.CustomerReportsController;
import client_controllers.CustomerServiceController;
import client_controllers.DefineTypeController;
import client_controllers.DefineCategoryController;
import client_controllers.EmployeeLoginController;
import client_controllers.InitiateMarketingController;
import client_controllers.MaintainAreaController;
import client_controllers.MarketingEmployeeController;
import client_controllers.ProductController;
import client_controllers.SalesManController;
import entity.AddressingCustomer;
import entity.Area;
import entity.Boss;
import entity.CampaignPattern;
import entity.Classification;
import entity.Customer;
import entity.Employee;
import entity.MarketingCampaign;
import entity.Product;
import entity.Type;

/**
 * This class overrides some of the methods defined in the abstract
 * superclass in order to give more functionality to the client.
 *

 */
public class CcrmCon extends ObservableClient
{
  //Instance variables **********************************************
  Object currCon;
  /**
   * The interface type variable.  It allows the implementation of 
   * the display method in the client.
   */
 

  
  //Constructors ****************************************************
  
  /**
   * Constructs an instance of the chat client.
   *
   * @param host The server to connect to.
   * @param port The port number to connect on.
   * @param clientUI The interface type variable.
   */
  
  public CcrmCon(String host, int port) 
    throws IOException 
  {
    super(host, port); //Call the superclass constructor
   // System.out.println("host: "+host +" port" +port);
    
    openConnection();
  }

  
  //Instance methods ************************************************
    
  /**
   * This method handles all data that comes in from the server.
   *
   * @param msg The message from the server.
   */
  public void handleMessageFromServer(Object msg) 
  {
	  Boss b=(Boss) msg;
	  System.out.println("handleMessageFromServer");

	  if(b.getObj() instanceof Employee){  //login
		  if(b.getControl().equals("EmployeeLoginController")){
			  ((EmployeeLoginController) currCon).HandleMsg(msg);   
		  }	
		  else if(b.getControl().equals("CustomerServiceController")){
			  ((CustomerServiceController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("CampaignPatternController")){
			  ((CampaignPatternController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("MarketingEmployeeController")){
			  ((MarketingEmployeeController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("SalesManController")){
			  ((SalesManController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("MaintainAreaController")){
			  ((MaintainAreaController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("ClassifyProductController")){
			  ((ClassifyProductController) currCon).HandleMsg(msg);
		  }
		  
		  
	  }
	  else if(b.getObj() instanceof Customer){
		  if(b.getControl().equals("CustomerServiceController")){ 
			  ((CustomerServiceController) currCon).HandleMsg(msg);
		  }
	  }
	  
	  
	  else if(b.getObj() instanceof ArrayList){
		  if(b.getControl().equals("CustomerServiceController")){ 
			  ((CustomerServiceController) currCon).HandleMsg(msg);
		  }

		  else if(b.getControl().equals("CampaignPatternController")){ 
			  ((CampaignPatternController) currCon).HandleMsg(msg);
		  }	 
		  else if(b.getCommand().equals("getCustomer") || b.getCommand().equals("getCampaign") || b.getCommand().equals("get saled product")|| b.getCommand().equals("getFuturePermission")|| b.getCommand().equals("New Sale or Deny")){
			  ((SalesManController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("ProductController")){ 
			  ((ProductController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("DefineCategoryController")){ 
			  ((DefineCategoryController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("DefineTypeController")){ 
			  ((DefineTypeController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("MaintainAreaController")){ 
			  ((MaintainAreaController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("ClassifyProductController")){ 
			  ((ClassifyProductController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("InitiateMarketingController")){ 
			  ((InitiateMarketingController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("CustomerReportsController")){ 
			  ((CustomerReportsController) currCon).HandleMsg(msg);
		  }
		  else if(b.getControl().equals("CampaignReportsController")){ 
			  ((CampaignReportsController) currCon).HandleMsg(msg);
		  }
		  
		 
	  }
	  
	  else if(b.getObj() instanceof CampaignPattern){
		  if(b.getControl().equals("CampaignPatternController")){ 
			  ((CampaignPatternController) currCon).HandleMsg(msg);
		  }
	  }
	  else if(b.getObj() instanceof Product){
		  if(b.getControl().equals("ProductController")){ 
			  ((ProductController) currCon).HandleMsg(msg);
		  }
	  }
	  
	  else if(b.getObj() instanceof Area){
		  if(b.getControl().equals("MaintainAreaController")){ 
			  ((MaintainAreaController) currCon).HandleMsg(msg);
		  }
	  }
	  else if(b.getObj() instanceof Classification){
		  if(b.getControl().equals("ClassifyProductController")){ 
			  ((ClassifyProductController) currCon).HandleMsg(msg);
		  }

	  }
	  else if(b.getObj() instanceof Type){
		  if(b.getControl().equals("DefineTypeController")){ 
			  ((DefineTypeController) currCon).HandleMsg(msg);
		  }
	  }
	  

	  else if(b.getObj() instanceof MarketingCampaign){
		  if(b.getControl().equals("InitiateMarketingController")){ 
			  ((InitiateMarketingController) currCon).HandleMsg(msg);
		  }
	  }
	  else if(b.getObj() instanceof AddressingCustomer){
		  if(b.getControl().equals("InitiateMarketingController")){ 
			  ((InitiateMarketingController) currCon).HandleMsg(msg);
		  }
	  }



	  else if(b.getObj() instanceof Type){

		  if(b.getControl().equals("InitiateMarketingController")){ 
			  ((InitiateMarketingController) currCon).HandleMsg(msg);
		  }

	  }
	  
	  else if(b.getCommand().equals("addCategoryToTable")){
		  if(b.getControl().equals("DefineCategoryController")){ 
			  ((DefineCategoryController) currCon).HandleMsg(msg);
		  }
 	  }
	  else if(b.getCommand().equals("analyze")){ 
			  ((AnaliticSystemController) currCon).HandleMsg(msg);
		  }
  }



  /**
   * This method terminates the client.
   */
  public void quit()
  {
    try
    {
      closeConnection();
    }
    catch(IOException e) {}
    System.exit(0);
  }
  
  public void setCurrCon(Object obj){
	  this.currCon=obj;
  }
}
//End of CcrmClient class



